<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Controllers
 * @see 		Contacts
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Contacts extends CI_Controller {
	var $q = '';
	var $limit = '';
	var $time_from = '';
	var $time_to = '';
	var $orderby = '';
	var $ordermode = '';
	var $updated = '';
	var $failed = '';
	var $error = '';
	var $name = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $site_url = '';
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Cont');
		$this->q = $this->input->get('q', true);
		$this->time_from = $this->input->get('from', true);
		$this->time_to = $this->input->get('to', true);
		$this->orderby = $this->input->get('orderby', true);
		$this->ordermode = $this->input->get('ordermode', true);
		$this->updated = $this->input->get('updated', true);
		$this->failed = $this->input->get('failed', true);
		$this->error = $this->input->get('error', true);
		$this->name = $this->input->get('name', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'],
			'q' => $this->q,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'limit' => $this->limit,
			'from' => $this->time_from, 
			'to' => $this->time_to,
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode
		);
		$this->uri_str = url_uri($this->uri_arr);
		$this->site_url = site_url($GLOBALS['var']['act']);
	}

	public function index()
	{
		/*
		* Xu ly du lieu
		*/
		$num_rows = $this->Cont->show($this->uri_arr, true);
		$data = array(
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode,
			'contacts' => array(),
			'site_url' => $this->site_url,
			'uri_str' => $this->uri_str
		);
		$rows = $this->Cont->show($this->uri_arr);
		if ($rows) $data['contacts'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Liên hệ khách hàng',
			'add_link' => '',
			'search' => true,
			'page_list' => page_list($num_rows, $this->uri_arr),
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function reply($id = '')
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if (!$id || !$GLOBALS['per']['edit'])
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$info = $this->Cont->info($id);
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'error' => $this->error,
			'name' => $this->name,
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str,
			'info' => $info,
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Trả lời liên hệ',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/reply', $data);
		$this->load->view('footer');
	}

	public function process()
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if (!$GLOBALS['per']['edit'])
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Kiem tra POST method
		*/
		if(!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$reply = $this->input->post('reply');
		$id = $this->input->post('id', true);
		if (!token_validation() || $reply == '' || !$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$title = $this->input->post('title', true);
		$this->uri_arr['name'] = str_replace('&', '', $title);
		/*
		* Xu ly du lieu
		*/
		$email = $this->input->post('email', true);
		$data = array(
			'reply' => $reply,
			'status' => 1,
			'title' => $title,
			'date_view' => 1,
			'date_modified' => date(TIME_SQL)
		);
		/*
		* Gui email
		*/
		$config = array(
			'protocol' => 'smtp',
	        'smtp_host' => $GLOBALS['cfg']['smtp_host'],
	        'smtp_port' => $GLOBALS['cfg']['smtp_port'],
	        'smtp_user' => $GLOBALS['cfg']['smtp_user'],
	        'smtp_pass' => $GLOBALS['cfg']['smtp_pass'],
	        'mailtype'  => 'html',
	        'charset' => 'utf-8',
	        'wordwrap' => TRUE
	    );
	    $this->load->library('email', $config);
	    $link = str_replace('quanly/', '', base_url());
		$reply = str_replace('../../', $link, $reply);
	    $img_contact = '<img border="0" width="3" height="3" src="'.$link.'image_load/contact?i='.$id.'">';
	    $this->email->from($GLOBALS['cfg']['smtp_user'], $GLOBALS['cfg']['smtp_mailer']);
	    $this->email->to($email);
	    $this->email->subject($title);
	    $this->email->message($reply.$img_contact);
	    if ($this->email->send())
	    {
	    	if ($this->Cont->process($data, $id))
			{
				$this->uri_arr['updated'] = 1;
			}
			else
			{
				$this->uri_arr['failed'] = 1;
			}
	    }
	    else
	    {
	    	$this->uri_arr['error'] = 1;
	    }
		/*
		* Chuyen huong
		*/
		redirect($GLOBALS['var']['act'].'/reply/'.$id.url_uri($this->uri_arr));
	}
}

/* End of file contacts.php */
/* Location: ./application/controllers/contacts.php */